package com.yan.zktest;





import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.AsyncCallback.StringCallback;

public class ZkClientTest implements Watcher {
	private static ZooKeeper zk;
	
    public static void main(String[] args) throws Exception {
    	 zk = new ZooKeeper("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183",4*1000,new ZkClientTest());
    	
    	zk.getChildren("/rootNode", true);
    	
    	System.out.println("initRegisterEvent,children:");
    	
    	zk.create("/rootNode/nodes5","node5".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

    	Thread.sleep(Long.MAX_VALUE);
	}
	
	@Override
	public void process(WatchedEvent event) {
		if (Event.EventType.NodeChildrenChanged == event.getType()){
			try {
				System.out.println("ReRegisterEvent,childrenChanged:" + zk.getChildren("/rootNode", true));
			} catch (KeeperException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			try {
				System.out.println("ReRegisterEvent,children?:" + event + zk.getChildren("/rootNode", true));
			} catch (KeeperException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	

	


}
